package com.cari.oa.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.cari.oa.domain.SoftBugEntity;
import com.cari.oa.util.StrUtils;

@Repository
public class SoftBugDao extends BaseDao<SoftBugEntity> {
	
    /**
    * Title: getPage
    * Description: 分页查询
    * @author    hjx 
    * @date       2013-1-30 
    * @param createTime  开始时间
    * @param endTime     结束时间
    * @param bugLevel    级别
    * @param expurgator  修订人
    * @param status      状态
    * @param moduleId    模块
    * @param projectId   项目
    * @param page        第几页
    * @param size        每页多少条
    * @return
     */
    public List<SoftBugEntity> getPage( String bugLevel, String expurgator,String status,String moduleId,String projectId,String userType, int page, int size) {
    	List<Object> params = new ArrayList<Object>();
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("from SoftBugEntity b where 1=1 ");
        if(StrUtils.isNotEmpty(bugLevel)) {
            stringBuilder.append("and b.bugLevel like ? ");
            params.add("%" + bugLevel + "%");
        }
        if(StrUtils.isNotEmpty(expurgator)) {
            stringBuilder.append("and b.expurgator like ? ");
            params.add("%" + expurgator + "%");
        }
        if(StrUtils.isNotEmpty(status)) {
            stringBuilder.append("and b.status = ? ");
            params.add(status);
        }else
        {
        	if(userType.equals("0"))
        	{
        		 stringBuilder.append(" and b.status not in ('new','reject') ");
        	}
        }
        if(StrUtils.isNotEmpty(moduleId)) {
            stringBuilder.append("and b.moduleId = ? ");
            params.add(moduleId);
        }
        if(StrUtils.isNotEmpty(projectId)) {
            stringBuilder.append("and b.projectId = ? ");
            params.add(projectId);
        }
        stringBuilder.append(" order by b.createTime desc ");
        return getPage(stringBuilder.toString(), page, size, params.toArray());
    }
    
    /**
    * Title: getCount
    * Description:查询的记录数
    * @author    hjx 
    * @date       2013-1-30 
    * @param createTime  开始时间
    * @param endTime     结束时间
    * @param bugLevel    级别
    * @param expurgator  修订人
    * @param status      状态
    * @param moduleId    模块
    * @param projectId   项目
    * @return
    */
    public long getCount(String bugLevel, String expurgator,String status,String moduleId,String projectId,String userType) {
    	List<Object> params = new ArrayList<Object>();
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("select count(*) from SoftBugEntity b where 1=1 ");
        if(StrUtils.isNotEmpty(bugLevel)) {
            stringBuilder.append("and b.bugLevel like ? ");
            params.add("%" + bugLevel + "%");
        }
        if(StrUtils.isNotEmpty(expurgator)) {
            stringBuilder.append("and b.expurgator like ? ");
            params.add("%" + expurgator + "%");
        }
	    if(StrUtils.isNotEmpty(status)) {
            stringBuilder.append("and b.status = ? ");
            params.add(status);
        }else
        {
        	if(userType.equals("0"))
        	{
        		 stringBuilder.append(" and b.status not in ('new','reject') ");
        	}
        }
        if(StrUtils.isNotEmpty(moduleId)) {
            stringBuilder.append("and b.moduleId = ? ");
            params.add(moduleId);
        }
        if(StrUtils.isNotEmpty(projectId)) {
            stringBuilder.append("and b.projectId = ? ");
            params.add(projectId);
        }
        Query query = getSession().createQuery(stringBuilder.toString());
        for (int i = 0; i < params.size(); i++) {
            query.setParameter(i, params.get(i));
        }
        return (Long)query.uniqueResult();
    }

	@SuppressWarnings("unchecked")
	public boolean getByModuleId(String id) {
		    String hql="from SoftBugEntity s where moduleId = :moduleId"; 
	        Query query=getSession().createQuery(hql);    //创建查询
	        query.setString("moduleId",id);               //进行绑定
	        List<SoftBugEntity> list = new ArrayList<SoftBugEntity>();
	        list = query.list();                           //执行查询  
	        if(list != null && list.size() > 0)
	        {
	        	return false;
	        }       
	        return true;
	}
}
